﻿namespace WMS6.Web.Common
{
    using System;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Web.Caching;
    using System.Web.UI.HtmlControls;
    using WMS6.Web;

    public class RoleAjax : BasePage
    {
        protected HtmlForm form1;
        protected HtmlHead Head1;

        private void AssignFunctionResponse(string m_AssignFunctionData)
        {
            base.Response.Clear();
            base.Response.StatusCode = 200;
            base.Response.StatusDescription = "OK";
            base.Response.ContentType = "text/html";
            base.Response.Write(m_AssignFunctionData);
            base.Response.Flush();
            base.Response.End();
        }

        private void GetRoleData()
        {
            string id = base.Request.QueryString["id"];
            string Type = base.Request.QueryString["Type"];
            DataSet roleQueryEntity = null;
            if (base.Cache.Get("FUNCTION_ROLE") == null)
            {
                roleQueryEntity = RoleFunctionServiceClientProxy.GetInstance().GetRoleFunctionData("-1", base.UserInfo.Preference.Lang);
                base.Cache.Add("FUNCTION_ROLE", roleQueryEntity, null, DateTime.Now.AddMinutes(20.0), TimeSpan.Zero, CacheItemPriority.High, null);
            }
            else
            {
                roleQueryEntity = (DataSet) base.Cache["FUNCTION_ROLE"];
            }
            string CS40002 = Type;
            if (CS40002 != null)
            {
                if (!(CS40002 == "1"))
                {
                    if (((CS40002 == "2") || (CS40002 == "3")) || (CS40002 == "4"))
                    {
                        if (roleQueryEntity.Tables[0].Select(" PARENT_GROUP_GID ='" + id + "'").Count<DataRow>() > 0)
                        {
                            this.RoleDataListConversionHtml2(roleQueryEntity.Tables[0].Select(" PARENT_GROUP_GID ='" + id + "'"), "0");
                        }
                        else if (roleQueryEntity.Tables[1].Select(" GROUP_ID ='" + id + "'").Count<DataRow>() > 0)
                        {
                            this.RoleDataListConversionHtml3(roleQueryEntity.Tables[1].Select(" GROUP_ID ='" + id + "'"), "4");
                        }
                        else
                        {
                            this.RoleDataListConversionHtml4(roleQueryEntity.Tables[2].Select(" FUNCTION_ID='" + id + "'"));
                        }
                    }
                    else if (CS40002 == "5")
                    {
                        this.RoleDataListConversionHtml4(roleQueryEntity.Tables[2].Select(" FUNCTION_ID='" + id + "'"));
                    }
                }
                else
                {
                    this.GetSessionData();
                    this.RoleDataListConversionHtml1(roleQueryEntity.Tables[0].Select(" PARENT_GROUP_GID ='-1'"));
                }
            }
        }

        private void GetSessionData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["RoleId"]))
            {
                string StrRoleId = base.Request.QueryString["RoleId"];
                DataSet ds = RoleFunctionServiceClientProxy.GetInstance().GetRoleAndRoleFunctionDataByRoleId(StrRoleId);
                this.Session.Remove("RoleFunctionData");
                this.Session.Add("RoleFunctionData", ds);
            }
            else
            {
                this.Session.Remove("RoleFunctionData");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["Type"]))
            {
                this.GetRoleData();
            }
        }

        public void RoleDataListConversionHtml1(DataRow[] roleQueryEntity)
        {
            DataSet dsRoleInfo = null;
            if (this.Session["RoleFunctionData"] != null)
            {
                dsRoleInfo = this.Session["RoleFunctionData"] as DataSet;
            }
            StringBuilder StrHtml = new StringBuilder();
            string Type = base.Request.QueryString["Type"];
            for (int i = 0; roleQueryEntity.Count<DataRow>() > i; i++)
            {
                string StrColor = "color='gray'";
                string StrCheck = "<input type=\"checkbox\" id=\"{0}{1}{2}_CheckBox\" onclick=\"JavaScript:Check('{0}{1}{2}',0)\"  />";
                string StrStatus = "";
                if ((dsRoleInfo != null) && (dsRoleInfo.Tables[0].Rows.Count > 0))
                {
                    for (int j = 0; dsRoleInfo.Tables[0].Rows.Count > j; j++)
                    {
                        if ((roleQueryEntity[i][0].ToString() == dsRoleInfo.Tables[0].Rows[j]["A"].ToString()) && !(string.IsNullOrEmpty(dsRoleInfo.Tables[0].Rows[j]["B"].ToString()) || string.IsNullOrEmpty(dsRoleInfo.Tables[0].Rows[j]["D"].ToString())))
                        {
                            StrColor = "color='black'";
                            StrCheck = "<input type=\"checkbox\" id=\"{0}{1}{2}_CheckBox\" onclick=\"JavaScript:Check('{0}{1}{2}',0)\"  checked=\"true\" />";
                            StrStatus = "true";
                            break;
                        }
                    }
                }
                StrHtml.AppendFormat(" <table id=\"{0}{1}{2}_Parent\" cellpadding=\"0\" cellspacing=\"0\"  style=\"border-width:0;\"><tr><td>", roleQueryEntity[i][0], roleQueryEntity[i][1], Convert.ToInt32(Type) + 1);
                StrHtml.AppendFormat("<a id=\"{0}{1}{2}_Aclick\" onclick=\"JavaScript:Load('{0}','{1}','{2}','{3}')\"><img src=\"../Images/smalladdition.gif\" id=\"{0}{1}{2}_img\" alt=\"展开 {2}\" style=\"border-width:0;\" /></a></td><td style=\"white-space:nowrap;\">" + StrCheck + "<a  href=\"JavaScript:Load('{0}','{1}','{2}','{3}')\" id=\"{0}{1}{2}\"><font id=\"{0}{1}{2}_font\" {4}>{1}</font></a>", new object[] { roleQueryEntity[i][0], roleQueryEntity[i][1], Convert.ToInt32(Type) + 1, StrStatus, StrColor });
                StrHtml.AppendFormat("</td></tr><tr><td ></td><td align=left><div id=\"{0}{1}{2}_div\" style=\"display:none;\"></div></td></tr></table><input id=\"{0}{1}{2}_ClassHid\" type=\"hidden\"  />", roleQueryEntity[i][0], roleQueryEntity[i][1], Convert.ToInt32(Type) + 1);
            }
            this.AssignFunctionResponse(StrHtml.ToString());
        }

        public void RoleDataListConversionHtml2(DataRow[] roleQueryEntity, string Level)
        {
            DataSet dsRoleInfo = null;
            if (this.Session["RoleFunctionData"] != null)
            {
                dsRoleInfo = this.Session["RoleFunctionData"] as DataSet;
            }
            string Parent = base.Request.QueryString["Id"] + base.Request.QueryString["Name"] + base.Request.QueryString["Type"];
            string Type = base.Request.QueryString["Type"];
            StringBuilder StrHtml = new StringBuilder();
            string StrName = string.Empty;
            if (roleQueryEntity.Count<DataRow>() > 0)
            {
                for (int i = 0; roleQueryEntity.Count<DataRow>() > i; i++)
                {
                    object CS00001 = StrName;
                    StrName = string.Concat(new object[] { CS00001, roleQueryEntity[i][0], base.Server.HtmlEncode(roleQueryEntity[i][1].ToString()), Convert.ToInt32(Type) + 1, "," });
                    string StrColor = "color='gray'";
                    string StrCheck = "<input type=\"checkbox\" id=\"{0}{1}{2}_CheckBox\" onclick=\"JavaScript:Check('{0}{1}{2}',0)\"  />";
                    string StrStatus = "";
                    if ((dsRoleInfo != null) && (dsRoleInfo.Tables[0].Rows.Count > 0))
                    {
                        for (int j = 0; dsRoleInfo.Tables[0].Rows.Count > j; j++)
                        {
                            if (((roleQueryEntity[i][0].ToString() == dsRoleInfo.Tables[0].Rows[j]["B"].ToString()) || (roleQueryEntity[i][0].ToString() == dsRoleInfo.Tables[0].Rows[j]["C"].ToString())) && !string.IsNullOrEmpty(dsRoleInfo.Tables[0].Rows[j]["C"].ToString()))
                            {
                                StrColor = "color='black'";
                                StrCheck = "<input type=\"checkbox\" id=\"{0}{1}{2}_CheckBox\" onclick=\"JavaScript:Check('{0}{1}{2}',0)\"  checked=\"true\" />";
                                StrStatus = "true";
                                break;
                            }
                        }
                    }
                    StrHtml.AppendFormat(" <table id=\"{0}{1}{2}_Parent\" cellpadding=\"0\" cellspacing=\"0\"  style=\"border-width:0;\"><tr><td>", roleQueryEntity[i][0], roleQueryEntity[i][1], Convert.ToInt32(Type) + 1);
                    StrHtml.AppendFormat("<a id=\"{0}{1}{2}_Aclick\" onclick=\"JavaScript:Load('{0}','{1}','{2}','{3}')\"><img src=\"../Images/smalladdition.gif\" id=\"{0}{1}{2}_img\" alt=\"展开 {2}\" style=\"border-width:0;\" /></a></td><td style=\"white-space:nowrap;\">" + StrCheck + "<a  href=\"JavaScript:Load('{0}','{1}','{2}','{3}')\" id=\"{0}{1}{2}\"><font id=\"{0}{1}{2}_font\" {4}>{1}</font></a>", new object[] { roleQueryEntity[i][0], roleQueryEntity[i][1], Convert.ToInt32(Type) + 1, StrStatus, StrColor });
                    StrHtml.AppendFormat("</td></tr><tr><td ></td><td align=left><div id=\"{0}{1}{2}_div\" style=\"display:none;\"></div></td></tr></table><input id=\"{0}{1}{2}_ParentHid\" type=\"hidden\" value=\"{3}\" /><input id=\"{0}{1}{2}_ClassHid\" type=\"hidden\"  /><input id=\"{0}{1}{2}_CheckHid\" type=\"hidden\" value={0}  /><input id=\"{0}{1}{2}_Type\" type=\"hidden\" value={4} />", new object[] { roleQueryEntity[i][0], roleQueryEntity[i][1], Convert.ToInt32(Type) + 1, Parent, Level });
                }
            }
            else
            {
                StrHtml.Append(" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font  class=\"label_message\" >Sorry No Data</font>");
                StrName = ",";
            }
            this.AssignFunctionResponse(StrHtml.ToString() + "@_@" + StrName.Substring(0, StrName.Length - 1) + "@_@" + Parent);
        }

        public void RoleDataListConversionHtml3(DataRow[] roleQueryEntity, string Level)
        {
            DataSet dsRoleInfo = null;
            if (this.Session["RoleFunctionData"] != null)
            {
                dsRoleInfo = this.Session["RoleFunctionData"] as DataSet;
            }
            string Parent = base.Request.QueryString["Id"] + base.Request.QueryString["Name"] + base.Request.QueryString["Type"];
            string Type = Level;
            StringBuilder StrHtml = new StringBuilder();
            string StrName = string.Empty;
            if (roleQueryEntity.Count<DataRow>() > 0)
            {
                for (int i = 0; roleQueryEntity.Count<DataRow>() > i; i++)
                {
                    object CS00001 = StrName;
                    StrName = string.Concat(new object[] { CS00001, roleQueryEntity[i][0], base.Server.HtmlEncode(roleQueryEntity[i][1].ToString()), Convert.ToInt32(Type) + 1, "," });
                    string StrColor = "color='gray'";
                    string StrCheck = "<input type=\"checkbox\" id=\"{0}{1}{2}_CheckBox\" onclick=\"JavaScript:Check('{0}{1}{2}',0)\"  />";
                    string StrStatus = "";
                    if ((dsRoleInfo != null) && (dsRoleInfo.Tables[0].Rows.Count > 0))
                    {
                        for (int j = 0; dsRoleInfo.Tables[0].Rows.Count > j; j++)
                        {
                            if ((roleQueryEntity[i][0].ToString() == dsRoleInfo.Tables[0].Rows[j]["D"].ToString()) && !string.IsNullOrEmpty(dsRoleInfo.Tables[0].Rows[j]["C"].ToString()))
                            {
                                StrColor = "color='black'";
                                StrCheck = "<input type=\"checkbox\" id=\"{0}{1}{2}_CheckBox\" onclick=\"JavaScript:Check('{0}{1}{2}',0)\"  checked=\"true\" />";
                                StrStatus = "true";
                                break;
                            }
                        }
                    }
                    StrHtml.AppendFormat(" <table id=\"{0}{1}{2}_Parent\" cellpadding=\"0\" cellspacing=\"0\"  style=\"border-width:0;\"><tr><td>", roleQueryEntity[i][0], roleQueryEntity[i][1], Convert.ToInt32(Type) + 1);
                    StrHtml.AppendFormat("<a id=\"{0}{1}{2}_Aclick\" onclick=\"JavaScript:Load('{0}','{1}','{2}','{3}')\"><img src=\"../Images/smalladdition.gif\" id=\"{0}{1}{2}_img\" alt=\"展开 {2}\" style=\"border-width:0;\" /></a></td><td style=\"white-space:nowrap;\">" + StrCheck + "<a  href=\"JavaScript:Load('{0}','{1}','{2}','{3}')\" id=\"{0}{1}{2}\"><font id=\"{0}{1}{2}_font\" {4}>{1}</font></a>", new object[] { roleQueryEntity[i][0], roleQueryEntity[i][1], Convert.ToInt32(Type) + 1, StrStatus, StrColor });
                    StrHtml.AppendFormat("</td></tr><tr><td ></td><td align=left><div id=\"{0}{1}{2}_div\" style=\"display:none;\"></div></td></tr></table><input id=\"{0}{1}{2}_ParentHid\" type=\"hidden\" value=\"{3}\" /><input id=\"{0}{1}{2}_ClassHid\" type=\"hidden\"  /><input id=\"{0}{1}{2}_CheckHid\" type=\"hidden\" value={0}  /><input id=\"{0}{1}{2}_Type\" type=\"hidden\" value={4} />", new object[] { roleQueryEntity[i][0], roleQueryEntity[i][1], Convert.ToInt32(Type) + 1, Parent, Level });
                }
            }
            else
            {
                StrHtml.Append(" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font  class=\"label_message\" >Sorry No Data</font>");
                StrName = ",";
            }
            this.AssignFunctionResponse(StrHtml.ToString() + "@_@" + StrName.Substring(0, StrName.Length - 1) + "@_@" + Parent);
        }

        public void RoleDataListConversionHtml4(DataRow[] roleQueryEntity)
        {
            string Type = "5";
            DataSet dsRoleInfo = null;
            if (this.Session["RoleFunctionData"] != null)
            {
                dsRoleInfo = this.Session["RoleFunctionData"] as DataSet;
            }
            string Parent = base.Request.QueryString["Id"] + base.Request.QueryString["Name"] + base.Request.QueryString["Type"];
            StringBuilder StrHtml = new StringBuilder();
            string StrName = string.Empty;
            if (roleQueryEntity.Count<DataRow>() > 0)
            {
                for (int i = 0; roleQueryEntity.Count<DataRow>() > i; i++)
                {
                    object CS00001 = StrName;
                    StrName = string.Concat(new object[] { CS00001, roleQueryEntity[i][0], base.Server.HtmlEncode(roleQueryEntity[i][1].ToString()), Convert.ToInt32(Type) + 1, "," });
                    string StrColor = "color='gray'";
                    string StrCheck = "<input type=\"checkbox\" id=\"{0}{1}{2}_CheckBox\" onclick=\"JavaScript:Check('{0}{1}{2}',0)\"  />";
                    string StrStatus = "";
                    if ((dsRoleInfo != null) && (dsRoleInfo.Tables[0].Rows.Count > 0))
                    {
                        for (int j = 0; dsRoleInfo.Tables[0].Rows.Count > j; j++)
                        {
                            if (roleQueryEntity[i][0].ToString() == dsRoleInfo.Tables[0].Rows[j]["E"].ToString())
                            {
                                StrColor = "color='black'";
                                StrCheck = "<input type=\"checkbox\" id=\"{0}{1}{2}_CheckBox\" onclick=\"JavaScript:Check('{0}{1}{2}',0)\"  checked=\"true\" />";
                                StrStatus = "true";
                                break;
                            }
                        }
                    }
                    StrHtml.AppendFormat(" <table id=\"{0}{1}{2}_Parent\" cellpadding=\"0\" cellspacing=\"0\"  style=\"border-width:0;\"><tr><td td width=15px>", roleQueryEntity[i][0], roleQueryEntity[i][1], Convert.ToInt32(Type) + 1);
                    StrHtml.AppendFormat(" </td><td style=\"white-space:nowrap;\">" + StrCheck + "<a  href=\"#\" id=\"{0}{1}{2}\"><font id=\"{0}{1}{2}_font\" {4}>{1}</font></a>", new object[] { roleQueryEntity[i][0], roleQueryEntity[i][1], Convert.ToInt32(Type) + 1, StrStatus, StrColor });
                    StrHtml.AppendFormat("</td></tr><tr><td ></td><td align=left><div id=\"{0}{1}{2}_div\" style=\"display:none;\"></div></td></tr></table><input id=\"{0}{1}{2}_ParentHid\" type=\"hidden\" value=\"{3}\" /><input id=\"{0}{1}{2}_ClassHid\" type=\"hidden\" value='Lower'  /><input id=\"{0}{1}{2}_CheckHid\" type=\"hidden\" value={0}  />", new object[] { roleQueryEntity[i][0], roleQueryEntity[i][1], Convert.ToInt32(Type) + 1, Parent });
                }
            }
            else
            {
                StrHtml.Append(" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font  class=\"label_message\" >Sorry No Data</font>");
                StrName = "xxxxx,";
            }
            this.AssignFunctionResponse(StrHtml.ToString() + "@_@" + StrName.Substring(0, StrName.Length - 1) + "@_@" + Parent);
        }
    }
}

